package sas

import (
	"time"

	"gitee.com/micro-plat/sas/sas/modules/const/enum"
	"github.com/micro-plat/lib4go/types"
)

//Multi 混合签名和加密方法
//input 签名和加密参数
//signType 签名类型:md5,sha1,sha256,sha512,hmac
//encryptType 加密类型:rsa,aes,des
//mode 指定加密模式  例:aes加密时:CFB/pkca5   des加密时:CBC/pkca5 rsa加密时,指定密钥类型:pkcs8(这里不用传,加密密钥类型会在认证系统中自动获取)
func (n *sasClient) Multi(input types.XMap, signType, encryptType string, mode ...string) (sign string, data types.XMap, err error) {
	//签名和加密类型检查
	if err = n.checkType(signType, encryptType, enum.Multi); err != nil {
		return
	}

	input.SetValue(n.getTimespanName(), time.Now().Format("20060102150405"))
	input.SetValue(n.getEUIDName(), n.euid)
	encryptMap := types.XMap{}
	encryptMap, err = n.multiEncrypt(input, encryptType, mode...)
	if err != nil {
		return
	}

	return n.multiSign(encryptMap, signType)
}

func (n *sasClient) multiSign(input types.XMap, signType string) (sign string, data types.XMap, err error) {
	switch signType {
	case MD5Str:
		return n.md5Sign(input)
	case SHA1Str:
		return n.sha1Sign(input)
	case SHA256Str:
		return n.sha256Sign(input)
	case SHA512Str:
		return n.sha512Sign(input)
	case HMACStr:
		return n.hmacSign(input)
	}

	return
}

func (n *sasClient) multiEncrypt(input types.XMap, encryptType string, mode ...string) (data types.XMap, err error) {
	data = types.XMap{}
	switch encryptType {
	case AESStr:
		return n.AESEncrypt(input, mode...)
	case DESStr:
		return n.DESEncrypt(input, mode...)
	case RSAStr:
		return n.RSAEncrypt(input)
	}
	return
}
